<template>
  <div class="tabs">
    <!-- -->
    <el-tag
      v-for="(item, index) in tabList"
      :key="item.label"
      size="mini"
      :closable="item.name !== 'home'"
      :effect="$route.name === item.name ? 'dark' : 'plain'"
      @click="tagClick(item)"
      @close="tagClose(item, index)"
    >
      {{ item.label }}
    </el-tag>
  </div>
</template>

<script>
export default {
  name: "VueSysmangerCommTag",

  data() {
    return {};
  },
  computed: {
    tabList() {
      return this.$store.state.tab.tabList;
    },
  },
  mounted() {},

  methods: {
    tagClick(item) {
      if (item.name !== "home") {
        if (this.$router.name !== item.name) {
          this.$router.push(item.path);
        }
      }
    },
    //点击关闭
    tagClose(item, index) {
      console.log(item);
      this.$store.commit("removeTag", item);
      //如果点击的不是当前页面tag
      if (item.name !== this.$route.name) {
        return;
      }
      //点击的是最后一个tag
      if (this.tabList.length === index) {
        this.$router.push({
          name: this.tabList[index - 1].name,
        });
      } else {
        this.$router.push({
          name: this.tabList[index].name,
        });
      }
    },
  },
};
</script>
<style lang="less" scoped>
.tabs {
  padding: 10px 10px 0px 20px;
  .el-tag {
    margin-right: 15px;
    cursor: pointer;
  }
}
</style>